package com.slf.engine.services.helper;


import java.util.List;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.KeyedDigestMD5;
import com.zhkj.wsaction.type.TPacket;


public class ServiceHelper {

	private static final Logger log = Logger.getLogger(ServiceHelper.class);
	
	/**
	 * 补右空工具方法
	 * @param s 处理字符串
	 * @param len 字段规定长度
	 * @return 返回处理后的字符串
	 */
	public static String addRightNull(String s, int len)
	{
		if(s.length() == len)
		{
			return s;
		}
		int i = len - s.length();
		if(i >= 0)
		{
			for(int j = 0; j < i; j++)
			{
				s = s + " ";
			}
		}
		return s;
	}
	
	/**
	 * 补左0工具方法
	 * @param s 处理字符串
	 * @param len 字段规定长度
	 * @return 返回处理后的字符串
	 */
	public static String addLeftZero(String s, int len)
	{	
		if(s.length() == len)
		{
			return s;
		}
		int i = len - s.length();
		if(i >= 0)
		{
			for(int j = 0; j < i; j++)
			{
				s = "0" + s;
			}
		}
		return s;
	}
	
	public static String makeMsg(String body, String type)
	{
		StringBuffer s = new StringBuffer();
		s.append(addLeftZero(body.length()+"",4));
		s.append("RCHF");
		s.append(type);
		s.append(SysContants.RC_CZY);
		s.append(SysContants.RC_CZYPWD);
		String sign = KeyedDigestMD5.getKeyedDigest(body+SysContants.RC_KEY,"");
		s.append(sign);
		s.append(body);
		s.append("\n");
		return s.toString();
	}
	
	public static String makeBody(Order order)
	{
		StringBuffer s = new StringBuffer();
		s.append(addRightNull(SysContants.RC_LOGINNAME,13));
		s.append(SysContants.RC_PASSWORD);
		s.append(SysContants.PAY_TYPE);
		s.append("01");
		if("03".equals(SysContants.PAY_TYPE) && order.getAccount().startsWith("0"))
		{
			if(order.getAccount().startsWith("020"))
			{
				s.append(addRightNull("020", 4));
			}else{
				s.append(order.getAccount().substring(0, 4));
			}
		}else{
			s.append(addRightNull("", 4));
		}
		s.append(addRightNull(order.getAccount(),13));
		s.append(addLeftZero(Integer.parseInt(order.getMoney())*100+"", 16));
		
		return s.toString();
	}
	
	/**
	 * 放入查询队列
	 * @param order
	 * @return
	 */
//	public static boolean putToCache(Order order)
//	{
//		log.log(Priority.INFO, "进入查询等待orderId:" + order.getOrderId() + " account:" + order.getAccount() + " money:" + order.getMoney() + " clbz:" + order.getClbz());
//		WaitBean waitBean = new WaitBean();
//		synchronized (SysContext.QUERY_MAP) {
//			if(!SysContext.QUERY_MAP.containsKey(order.getOrderId()))
//			{
//				waitBean.setOrder(order);
//				waitBean.setIsStart(true);
//				waitBean.setTimeout(System.currentTimeMillis());
//				SysContext.QUERY_MAP.put(order.getOrderId(), waitBean);
//				waitBean.start();
//				return true;
//			}
//		}
//		return false;
//	}
//	
	/**
	 * 放入通知队列
	 */
	public static boolean putToNotice(Order order)
	{
		int i = order.getTzcs();
		order.setTzcs(i++);
		order.setNexttzsj(System.currentTimeMillis() + (long)Math.pow(order.getTzcs(),2));
		SysContext.NOTICE_LIST.add(order);
		return false;
	}
	
	public static Order putPacketToOrder(TPacket packet)
	{
		Order order = new Order();
		order.setOrderId(packet.readFromVarDataAsStr("CZID"));
		order.setAccount(packet.readFromVarDataAsStr("BCZSJH"));
		order.setMoney(String.valueOf(Integer.parseInt(packet.readFromVarDataAsStr("CZJE"))/100));
		order.setStarttime(DateUtils.strDate("yyyy-MM-dd HH:mm:ss"));
		return order;
	}
	
	public static void main(String args[])
	{
		System.out.println(addRightNull("", 4).length());
	}
}
